package com.icangya.admin;

import com.alibaba.fastjson.JSONObject;
import com.icangya.DAO.IUserFootDAO;
import com.icangya.Utils.ImageUtils;
import com.icangya.Utils.JSONUtils;
import com.icangya.Utils.UploadUtils;
import com.icangya.Value.StaticValue;
import com.icangya.domain.Admin;
import com.icangya.domain.AgentImage;
import com.icangya.domain.User;
import com.icangya.service.IAdminUserService;
import com.icangya.service.IAgentService;
import com.icangya.service.IFootUserService;
import com.icangya.service.IMoneyService;
import com.icangya.wxpay.utils.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.icangya.Value.StaticValue.ADMIN_PROXY_SESSION;


@CrossOrigin
@RestController
@RequestMapping(path = "/proxy/")
public class ProxyController {

    @Autowired
    private IAdminUserService iAdminUserService;
    @Autowired
    private IAgentService iAgentService;
    @Autowired
    private IFootUserService iFootUserService;
    @Autowired
    private IUserFootDAO iUserFootDAO;
    @Autowired
    private IMoneyService iMoneyService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private ImageUtils imageUtils = ImageUtils.getIstance();

    @RequestMapping(value = "login")
    public JSONObject login(@RequestParam String name, @RequestParam String passWord, HttpServletRequest request,HttpSession session){
//        Admin admin= (Admin) session.getAttribute(ADMIN_USER_SESSION);//获取登录后台的用户信息

        //正则匹配密码为4-10位的数字
//        Pattern p = Pattern.compile("(\\d{4})|(\\d{5})|(\\d{6})|(\\d{7})|(\\d{8})|(\\d{9})|(\\d{10})");
        Pattern p = Pattern.compile("(\\d{4,10})");
        Matcher m = p.matcher(name);

        //正则匹配密码为6-12位的数字和字母
        Pattern p1 = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$");
        Matcher m1 = p1.matcher(passWord);

        if (m.matches() && m1.matches()) {
            //密码加密(规则：密码+proxy_key，使用md5加一次密)
            try {
                passWord = MD5Utils.getMD5Upper(passWord + StaticValue.PROXY_KEY);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            JSONObject jsonObject=new JSONObject();
            Admin admin=iAdminUserService.login(name,passWord,session);

            if(admin==null){
                //不存在该用户
                return JSONUtils.getRoot(1,"fail","不存在该用户");
            }else{
                if(passWord.equals(admin.getPassWord())){
                    /*登录成功,将用户写入session中*/
                    request.getSession().setAttribute(ADMIN_PROXY_SESSION,admin);
                    return  JSONUtils.getRoot(0,"success","登录成功");
                }else{
                    //密码不正确
                    return JSONUtils.getRoot(1,"fail","密码不正确");
                }
            }
        }else{
//            1，密码不能全部是数字
//            2，密码不能全部是字母
//            3，密码必须是数字或字母
            return JSONUtils.getRoot(1,"fail","账号或密码长度不符合，账号的长度是4-10的数字，密码的长度为6-12位的数字加字母");
        }
    }

    //获取登录后用户信息
    @RequestMapping(value = "loginIndex")
    public JSONObject loginIndex(HttpSession session){
        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
//        if(admin==null)
//            return JSONUtils.getRoot(1,"fail","用户不能为空");
        return JSONUtils.getRoot(0,"success",admin.getUserName());
//        return JSONUtils.getRoot(0,"success",admin);
    }

    @RequestMapping(value = "exitLogin")
    public JSONObject  exitLogin(HttpSession session){

        // 销毁session
        session.removeAttribute(ADMIN_PROXY_SESSION);
        return JSONUtils.getRoot(0,"success",null);
    }
    //修改密码
    @RequestMapping(value = "setAccountName")
    public JSONObject setAccountName(@RequestParam String password,@RequestParam String passWord2,HttpSession session){

        if(passWord2=="" || password =="")
            return JSONUtils.getRoot(1,"fail","参数不能为空");

        //正则匹配密码为4-10位的数字
        //        Pattern p = Pattern.compile("(\\d{4})|(\\d{5})|(\\d{6})|(\\d{7})|(\\d{8})|(\\d{9})|(\\d{10})");
        //        Matcher m = p.matcher(name);

        //正则匹配密码为6-12位的数字和字母
        Pattern p1 = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$");
        Matcher m = p1.matcher(password);
        Matcher m1 = p1.matcher(passWord2);

        if (m.matches()) {
            //密码加密(规则：密码+proxy_key，使用md5加一次密)
            try {
                password = MD5Utils.getMD5Upper(password + StaticValue.PROXY_KEY);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }else{
//            1，密码不能全部是数字
//            2，密码不能全部是字母
//            3，密码必须是数字或字母
            return JSONUtils.getRoot(1,"fail","旧密码长度不符合 ，密码的长度为6-12位的数字加字母");
        }

        if (m1.matches()) {
            //密码加密(规则：密码+proxy_key，使用md5加一次密)
//            passWord = MD5Util.toMD5(passWord + StaticValue.PROXY_KEY).toUpperCase();
            try {
                passWord2 = MD5Utils.getMD5Upper(passWord2 + StaticValue.PROXY_KEY);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

        }else{
//            1，密码不能全部是数字
//            2，密码不能全部是字母
//            3，密码必须是数字或字母
            return JSONUtils.getRoot(1,"fail","新密码长度不符合 ，密码的长度为6-12位的数字加字母");
        }
        logger.info("==="+password);
        logger.info("==="+passWord2);
//        Admin admin=  iAdminDAO.getAdminById(1); //测试
        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
//        return iAdminUserService.setAccountName("xsrkeji1109",password,passWord2);//测试
        return iAdminUserService.setAccountName(admin.getUserName(),password,passWord2);


    }



    /**
     * 获取用户详情
     * @return
     */
    @RequestMapping(value = "getUserAll")
    public JSONObject getUserAll(@RequestParam int  startPage,HttpSession session){

        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
//        if(admin==null){
//            return JSONUtils.getRoot(1,"fail","用户为空");
//        }
//        JSONObject jsonObject=iAdminUserService.getUserByProxy(startPage,admin.getUid());
        JSONObject jsonObject=iAdminUserService.getUserByProxy(startPage,1);

        if(jsonObject!=null)
            return JSONUtils.getRoot(0,"success",jsonObject);

        return JSONUtils.getRoot(1,"fail",null);
    }

    /**
     *  根据用户id,查询用户信息
     * @param uid
     * @return
     */
    @RequestMapping(value = "checkUserById")
    public JSONObject checkUserById(@RequestParam long uid,HttpSession session){

        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
        //        if(admin==null){
//            return JSONUtils.getRoot(1,"fail","用户为空");
//        }

        JSONObject jsonObject=iAdminUserService.checkUserById(uid,admin.getUid());
        if(jsonObject!=null)
            return JSONUtils.getRoot(0,"success",jsonObject);

        return JSONUtils.getRoot(1,"fail",null);
    }


    /**
     * 获取用户踢球的所有记录
     * @param openId
     * @return
     */
    @RequestMapping(value = "getKickBallByOpenId")
    public JSONObject getKickBallByOpenId(@RequestParam String openId,@RequestParam int startPage){

        if("".equals(openId) || startPage < 1){
            return  JSONUtils.getRoot(1,"fail","参数有误");
        }

        JSONObject jsonObject=  iAdminUserService.getKickBallByOpenId(openId,startPage);
        if(jsonObject==null)
            return JSONUtils.getRoot(1,"fail","该用户没有踢球记录");

//        jsonObject.put("order",order+1);
        return JSONUtils.getRoot(0,"success",jsonObject);
    }

    /**
     * 用户提现的记录
     * @param openId
     * @return
     */
    @RequestMapping(value = "getCashRecord")
    public JSONObject getCashRecord(@RequestParam String openId,@RequestParam int startPage){

        if("".equals(openId) || startPage < 1){
            return  JSONUtils.getRoot(1,"fail","参数有误");
        }

        JSONObject jsonObject=iMoneyService.getCashRecord(openId,startPage);
        if(jsonObject==null)
            return JSONUtils.getRoot(1,"fail","用户没有提现记录");

//        jsonObject.put("order",order+1);
        return JSONUtils.getRoot(0,"success",jsonObject);
    }

    /**
     * 获取用户佣金明细
     * @param openId
     * @return
     */
    @RequestMapping(value = "getCommRecord")
    public JSONObject getCommRecord(@RequestParam String openId,@RequestParam int startPage){

        if( "".equals(openId) || startPage < 1){
            return  JSONUtils.getRoot(1,"fail","参数有误");
        }
        JSONObject jsonObject=   iMoneyService.getCommRecord(openId,startPage);
        if(jsonObject==null)
            return JSONUtils.getRoot(1,"fail","该用户没有佣金记录");

//        jsonObject.put("order",order+1);
        return JSONUtils.getRoot(0,"success",jsonObject);
    }


    /**
     * 广告金记录,支出记录
     * @param openId
     * @return
     */
    @RequestMapping(value = "getExpendRecordMoney")
    public JSONObject getExpendRecordMoney(@RequestParam String openId,@RequestParam int startPage){

        if( "".equals(openId) || startPage < 1){
            return  JSONUtils.getRoot(1,"fail","参数有误");
        }

        JSONObject jsonObject=iMoneyService.getExpendRecordMoney(openId,startPage);
        if(jsonObject==null)
            return JSONUtils.getRoot(1,"fail","该用户没有广告金支出记录");

//        jsonObject.put("order",order+1);
        return JSONUtils.getRoot(0,"success",jsonObject);

    }

    /**
     * 广告金记录,充值记录
     * @param openId
     * @return
     */
    @RequestMapping(value = "getRechargeRecordMoney")
    public JSONObject getRechargeRecordMoney(@RequestParam String openId,@RequestParam int startPage){

//        if(order<0 || "".equals(openId) || startPage < 1){
//            return  JSONUtils.getRoot(1,"fail","参数有误");
//        }
        if( "".equals(openId) || startPage < 1){
            return  JSONUtils.getRoot(1,"fail","参数有误");
        }
        JSONObject jsonObject=iMoneyService.getRechargeRecordMoney(openId,startPage);
        if(jsonObject==null)
            return JSONUtils.getRoot(1,"fail","该用户没有广告金充值记录");

//        jsonObject.put("order",order+1);
        return JSONUtils.getRoot(0,"success",jsonObject);

    }


    /**
     * 我的团队信息
     * @param uid
     * @return
     */
    @RequestMapping(value = "getProxyNum")
    public JSONObject getProxyNum( @RequestParam long uid){

        return JSONUtils.getRoot(0,"success",iFootUserService.getProxyNum(uid));
    }

    /**
     * 查询用户，为其添加代理权限
     * @param uid
     * @param session
     * @return
     */
    @RequestMapping(value = "getAgentUser", method = RequestMethod.GET)
    public JSONObject getAgentUser(@RequestParam long uid,HttpSession session){
        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
        if(admin==null){
            return JSONUtils.getRoot(1, "fail", "用户为空");
        }
        if(admin.getUid()==uid){
            return JSONUtils.getRoot(1, "fail", "不能为自己设置代理");
        }
        if(iAgentService.checkAgentUser(uid)){
            return JSONUtils.getRoot(1, "fail", "该用户已经是代理了");
        }

//        JSONObject jsonObject=iAdminUserService.checkUserById(uid,1);//测试
        JSONObject jsonObject=iAdminUserService.checkUserById(uid,admin.getUid());
        if(jsonObject!=null){
            return JSONUtils.getRoot(0,"success",jsonObject);
        }
        return JSONUtils.getRoot(1,"fail","该用户名不在您的团队中");

    }


    /**
     *增加代理
     * @param name 登录名
     * @param passWord  密码
     * @param uid 游戏id
     * @return
     */
    @RequestMapping(value = "addAgent", method = RequestMethod.GET)
    public JSONObject addAgent(@RequestParam String name, @RequestParam String passWord, @RequestParam long uid, HttpSession session){


        //先判断该用户名是否已存在
        if (iAgentService.checkAgentName(name)) {
            return JSONUtils.getRoot(1, "fail", "用户名已存在");
        }

        if(iAgentService.checkAgentUser(uid)) {
            return JSONUtils.getRoot(1, "fail", "该用户已经是代理了");
        }

        Admin loginUser= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息

        if(loginUser.getUserLevel()>=5) {
            return JSONUtils.getRoot(1, "fail", "该用户没有权限开代理");
        }

        Admin user=iAgentService.getAdminById(loginUser.getId());
//        Admin user=iAgentService.getAdminById(1L);//测试登录的用户

        if(user==null){
            return JSONUtils.getRoot(1,"fail","用户为空，请重新登录");
        }

        //正则匹配密码为4-10位的数字
        Pattern p = Pattern.compile("(\\d{4,10})");
        Matcher m = p.matcher(name);

        //正则匹配密码为6-12位的数字和字母
        Pattern p1 = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$");
        Matcher m1 = p1.matcher(passWord);

        if (m.matches() && m1.matches()) {
            //密码加密(规则：密码+proxy_key，使用md5加一次密)
//            passWord = MD5Util.toMD5(passWord + StaticValue.PROXY_KEY).toUpperCase();
            try {
                passWord = MD5Utils.getMD5Upper(passWord + StaticValue.PROXY_KEY);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

        }else{
//            1，密码不能全部是数字
//            2，密码不能全部是字母
//            3，密码必须是数字或字母
            return JSONUtils.getRoot(1,"fail","账号或密码长度不符合，账号的长度是4-10的数字，密码的长度为6-12位的数字加字母");
        }

//        if (name.length()>10 || name.length()<4){
//            return JSONUtils.getRoot(1,"fail","账号长度不符合，请设置为4-10的长度");
//        }
//
//        if(passWord.length() < 6 || passWord.length() > 12){
//            return JSONUtils.getRoot(1,"fail","密码长度不符合，请设置为6-12的长度");
//        }

        //登录后台的账号，密码，用户游戏id
        // 修改该用户的proxyId为自己的游戏id
        //将添加的代理加到代理明细表中
        Admin admin = new Admin();
        admin.setUserName(name);
        admin.setPassWord(passWord);
        admin.setGradeId(1);//设置管理员
        admin.setUid(uid);
        admin.setUserId(user.getId());
        admin.setUserLevel(user.getUserLevel()+1);//树结构的层级数  父节点的层级加一
        admin.setCreateTime(new Timestamp(new Date().getTime()));
        //一、 左下标值= 父节点的右下标值
        //右下标值=父节点的右下标值+1
        //设置新节点左下标
        admin.setLeftNum(user.getRightNum());//父节点的右下标值
        admin.setRightNum(user.getRightNum() + 1);//父节点的右下标值+1
        admin.setCreateTime(new Timestamp(new Date().getTime()));


        //添加代理
        Admin use=iAgentService.addAgentUser(admin);
        if(use !=null){//添加成功之后

            //重新生成代理推广二维码
            setQrCode(use.getUid());

            // 二、更新：大于等于新增节点的左下标值的右下标值都加2
            iAgentService.updateAgentRightNum(use.getLeftNum(),use.getId());
            //三、更新：大于等于新增节点的右下标值的左下标值都加2
            iAgentService.updateAgentLeftNum(use.getRightNum(),use.getId());

            //修改所有粉丝的代理id为指定id
            changProxyId(use.getUid());


            return JSONUtils.getRoot(0,"success","添加成功");
        }

        return JSONUtils.getRoot(2,"fail","添加失败,请重新加载!!!!");
    }

    /**
     * 查询所有下级代理明细
     * @return
     */
    @RequestMapping(value = "findLowerAgent", method = RequestMethod.GET)
    public JSONObject findLowerAgent(@RequestParam String startTime,@RequestParam String endTime,@RequestParam int startPage,HttpSession session){


        logger.info(startTime+"开始时间 start..................end="+endTime);
        if(startTime ==null || startTime=="" )
            return JSONUtils.getRoot(1,"fail","开始时间未设置");
        if(endTime=="")
            return JSONUtils.getRoot(1,"fail","结束时间未设置");

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//

        Date start= new Date();
        Date end= new Date();
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
            System.out.println(start.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Timestamp sTime=new Timestamp(start.getTime());
        Timestamp eTime=new Timestamp(end.getTime());

        Admin loginUser= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
        Admin user=iAgentService.getAdminById(loginUser.getId());
//        Admin user=iAgentService.getAdminById(1L);//测试登录的用户

        JSONObject jsonObject=iAgentService.getLowerAgent(user.getId(),sTime,eTime,startPage);
        if(jsonObject!=null)
            return JSONUtils.getRoot(0,"success",jsonObject);
        return JSONUtils.getRoot(1,"fail",null);
    }

    //转换时间
    public Date getDate(String date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
        Date start= new Date();
        try {
            start = sdf.parse(date);
            logger.info(start.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return start;
    }


    /**
     * 取消授权代理
     * @param id
     * @return
     */
    @RequestMapping(value = "deleteAgent")
    public JSONObject deleteAgent(@RequestParam long id,HttpSession session){

        //  Admin user= (Admin) session.getAttribute(ADMIN_USER_SESSION);//获取登录后台的用户信息

        //取消授权之后他的粉丝的proxyId改为上级的授权代理proxyId
        //删除之后将该代理下的所有粉丝修改为系统的粉丝
        List<Object[]> list=iAgentService.deleteAgent(id);
        if(list==null)
            return JSONUtils.getRoot(0,"success","已取消授权");

        //取消之后将用户团队的推广二维码更新
        for(int i=0;i<list.size();i++){
            Object[] obj=list.get(i);
            if(obj[0]==null)
                continue;
            logger.info("取消代理授权更新二维码的用户====================="+obj[0]);
            setQrCode(Long.parseLong(obj[0].toString()));
        }
        return JSONUtils.getRoot(1,"fail","取消授权失败");
    }

    /**
     * 根据id查询代理信息
     * @param id
     * @return
     */
    @RequestMapping(value = "getAgentById")
    public JSONObject getAgentById(@RequestParam long id,@RequestParam String startTime,@RequestParam String endTime,HttpSession session){
        logger.info("getAgentById..........");
        logger.info(startTime+"时间 start..................end="+endTime);

        if(startTime ==null || startTime=="" )
            return JSONUtils.getRoot(1,"fail","开始时间未设置");
        if(endTime=="")
            return JSONUtils.getRoot(1,"fail","结束时间未设置");

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
        Date start= new Date();
        Date end= new Date();
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Timestamp sTime=new Timestamp(start.getTime());
        Timestamp eTime=new Timestamp(end.getTime());
        Admin loginUser= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
        Admin user=iAgentService.getAdminById(loginUser.getId());
//        Admin user=iAgentService.getAdminById(1L);//测试登录的用户
        JSONObject jsonObject=  iAgentService.getAgentById(id,sTime,eTime,user.getLeftNum(),user.getRightNum());
        if(jsonObject!=null)
            return JSONUtils.getRoot(0,"success",jsonObject);

        return JSONUtils.getRoot(1,"fail","该代理用户不存在");
    }


    /**
     * 代理客服图
     * @param request
     * @param file
     * @return
     */
    @RequestMapping(value = "setAgentImg")
    public JSONObject setAgentImg(HttpSession session, HttpServletRequest request, HttpServletResponse response, MultipartFile file) {

        logger.info(file+"==="+file.isEmpty());
        if (file.isEmpty()){
            try {
                response.sendRedirect("/agenter/code.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
//            return  JSONUtils.getRoot(1,"fail","文件不能为空！");
        }
        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
//        Admin user=iAgentService.getAdminById(loginUser.getId());
        String path="agentImg/";

        String agentImgUrl= UploadUtils.uploadImg(path,request,file);
        if(agentImgUrl==null)
            return JSONUtils.getRoot(1,"fail","图片上传失败");

        logger.info("agentImgUrl===================="+agentImgUrl);
//        AgentImage agentImage=new AgentImage(agentImgUrl,admin.getUid());
        AgentImage agentImage=new AgentImage(agentImgUrl,1);//测试
        AgentImage agent= iAgentService.addAgentImage(agentImage);
        if(agent!=null) {
            try {
//                response.sendRedirect("http://192.168.0.121:8020/football/src/main/resources/static/proxy/code.html");
                response.sendRedirect("/agenter/code.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return JSONUtils.getRoot(0, "success", agent);
        }
        return JSONUtils.getRoot(1,"fail","发布失败");
    }


    /**
     * 获取代理客服图
     * @return
     */
    @RequestMapping(value = "getAgentImg")
    public JSONObject getAgentImg(HttpSession session) {

        Admin admin= (Admin) session.getAttribute(ADMIN_PROXY_SESSION);//获取登录后台的用户信息
        AgentImage agent= iAgentService.getAgentImage(admin.getUid());
//        AgentImage agenter= iAgentService.getAgentImage(1);
        if(agent!=null)
            return JSONUtils.getRoot(0, "success", agent);
        return JSONUtils.getRoot(1,"fail","发布失败");

    }


    //子节点
    List<User> childUser = new ArrayList<User>();

    /**
     * 修改所有粉丝的代理id为指定id
     * @param uid
     * @return
     */
//    @RequestMapping(value = "changProxyId")
    public JSONObject changProxyId (long uid) {
        List<User> userList = iFootUserService.findAllUser();
        List<User> userList2 = new ArrayList<User>();
        treeUserList(userList,uid);

        logger.info(childUser.size()+"============");

        for (User user: childUser){
            // User user = userList2.get(i);
            user.setProxyId(uid);

            iUserFootDAO.updateProxyId(user.getId(),uid);

            //重新生成用户推广二维码
            setQrCode(user.getId());
            System.out.println(user.getId());

        }


        childUser = new ArrayList<User>();
        userList2 = null;
        return JSONUtils.getRoot(0,"success","");
    }


    //更新代理下面用户的二维码
    public   void setQrCode(long uid){

        new Thread(new Runnable() {
            @Override
            public void run() {
                logger.info("===========uid:"+uid+"============更新代理下面用户的二维码==============================");
                User needQR = iFootUserService.getUserByUid(uid);
                String shareUrl = null;
                try {
                    shareUrl = StaticValue.WX.ENTER_URL+"parentID=" + needQR.getId() + "&proxyID=" + needQR.getProxyId();
                    logger.info("shareUrl : " + shareUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                logger.info("这是测试登录:  QRCode=" + shareUrl);
                String SB = imageUtils.GetQR_BASE64(shareUrl);
//                    logger.info(SB);
                needQR.setQrCode(SB);
                iFootUserService.updateUser(needQR);//更新一下二维码图片 完成注册操作
            }
        }).start();

    }



    /**
     * 获取某个父节点下面的所有子节点
     * @param userList
     * @param pid
     * @return
     */
    public  List<User> treeUserList( List<User> userList, Long pid){

        for (User user: userList) {
            //遍历出父id等于参数的id，add进子节点集合
            if(user.getParentId()==pid && user.getProxyId()!=user.getId()){
                logger.info("add user for id : "+user.getId());
                treeUserList(userList,user.getId());
                childUser.add(user);
            }
        }
        return childUser;
    }







}
